package org.yourscrum.domain;

import javax.jdo.annotations.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Jettro Coenradie
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Task {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName="datanucleus", key="gae.encoded-pk",value="true")
    private String encodedKey;

    @Persistent
    @Extension(vendorName = "datanucleus", key = "gae.pk-id", value = "true")
    private Long id;

    @Persistent
    private Story story;

    @Persistent
    private String description;

    @Persistent
    private int estimatedHours = 0;

    @Persistent
    private int actualHours = 0;

    @Persistent
    private List<String> workDone;

    /* Constructors */
    public Task(Story story, String description) {
        this.story = story;
        this.description = description;
    }

    public void changeDescription(String description) {
        this.description = description;
    }

    public void changeEstimatedHours(int estimatedHours) {
        this.estimatedHours = estimatedHours;
    }

    public synchronized void logWorkDone(int actualHours, String descriptionOfWork) {
        if (workDone == null) {
            this.workDone = new ArrayList<String>();
        }
        this.actualHours += actualHours;
        workDone.add(descriptionOfWork);
    }


    /* Getter */
    public Long getId() {
        return id;
    }

    public Story getStory() {
        return story;
    }

    public String getDescription() {
        return description;
    }

    public int getEstimatedHours() {
        return estimatedHours;
    }

    public int getActualHours() {
        return actualHours;
    }

    public List<String> getWorkDone() {
        return Collections.unmodifiableList(workDone);
    }
}
